/**
 * Created by Administrator on 2017/7/4 0004.
 */
require(["jquery","paging","bbcare","jquery.dataTables",'bootstrap.dataTables',
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"],function($,Paging, $bb,$jTable,$bTable,$servers) {
    var URL = $bb.baseUrl("followup")+"followup/service";
    var obj=$bb.getPageParams();
    var userId=obj.userId;
    var type=obj.type;//历史记录传参；
    var count =1, pagesize =20;
    var HashMap=$servers.hashmap(),hashMap = new HashMap();
    var historyTitle=$("#historyTitle");

    getBabyHistoryList(1, 20);
    var p = new Paging();
    p.init({target:'#pageTool',pagesize:pagesize,count:count,toolbar:false,callback:function(page,size,count){
        getBabyHistoryList(page, size);
    }});
    $("#dataList").on('click',"ul li a",function () {
        var answerId = $(this).attr("result");
        if($(this).attr("aim") == "view"){
            var data = hashMap.get(answerId);
            obj.answerId = answerId;
            obj.answer = data;
            var testTime = $(this).parent("li").attr("testTime");
            obj.testTime = testTime;
            obj.business = $(this).parent("li").attr("business");
            if(type=='PEABODY') {
                $bb.openPage("peabodyScale",obj);
            }
        }else if($(this).attr("aim") == "viewReport"){
            if(type=='PEABODY') {
                obj.answerId = answerId;
                $bb.openPage("peabodyScaleResult",obj);
            }
        }
    });
    function getBabyHistoryList(page, rows) {
        var params={
            "userId": userId,
            "examinationPaperName": type,
            "page": page,
            "rows": rows
        };
        if(type=='PEABODY') {
            $(".resShow").css("display","none");
            historyTitle.html("peabody历史评测记录");
        }
        $bb.ajax({
            url: URL+"/patient/evaluation/getHistoryScore",//baby地址
            async: false,
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if (data.success) {
                    hashMap = new HashMap();
                    count = data.total;
                    var str = '';
                    for (var i = 0; i < data.rows.length; i++) {
                        var res = {
                            "answerResult":data.rows[i].answerResult,
                            "result":data.rows[i].result
                        };
                        hashMap.put(data.rows[i].answerId, res);
                        var actualDay = $servers.dateDiff(data.rows[i].birthday,data.rows[i].createTime.substring(0,10));
                        var age = $servers.age(actualDay, data.rows[i].pregnancyWeek);
                        var typeQues =data.rows[i].type == "PEABODY" ? "peabody运动量表":"";

                        str += '<ul class="clearfix" >';
                        str += '<li class="name">' + data.rows[i].babyName + '</li>';
                        str += '<li>' + age + '</li>';
                        str += '<li>' + data.rows[i].phoneNo+ '</li>';
                        str += '<li class = "quesType">' + typeQues + '</li>';
                        str += '<li class = "quesTime">' + data.rows[i].createTime + '</li>';
                        if(type == "PEABODY"){
                            str += '<li testTime = "'+data.rows[i].testTime+'" business = "'+data.rows[i].business+'"><a aim = "view" result="' + data.rows[i].answerId + '">评估问卷</a>&nbsp;&nbsp;<a aim = "viewReport" result="' + data.rows[i].answerId + '">评估报告</a></li>';
                        }
                        str += '</ul>';
                    }
                    $("#dataList").html(str);
                }
            }
        });
    }
});